package com.siblings.loan.ui.user.act

import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.lib.base.utils.helper.ActivityStackManager
import com.siblings.loan.R
import com.siblings.loan.databinding.ActMessageBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.ui.steps.act.SibStepsAct
import com.siblings.loan.ui.tab.act.SibMainAct
import com.siblings.loan.ui.user.adapter.SibMessageAdapter
import com.siblings.loan.ui.user.impl.SibMessagePresenterImpl
import com.siblings.loan.ui.user.view.ISibMessageView
import com.siblings.loan.utils.dialog.KefuDialog
import com.siblings.loan.utils.event.SibEventData
import com.siblings.loan.utils.event.SibEventType
import com.siblings.loan.utils.helper.SharedHelper
import com.siblings.loan.utils.helper.TestDataLibInvoke
import com.siblings.mvp.MvpAct
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

/**
 * SiblingsLoan
 * @Author wwdeng
 * @CreateTime: 2024/7/18 18:12
 * @Annotation：消息列表
 */
class SibMessageAct: MvpAct<ISibMessageView,SibMessagePresenterImpl>(),ISibMessageView {
    lateinit var binding: ActMessageBinding

    private var messageAdapter: SibMessageAdapter? = null

    override fun createMvpPresenter(): SibMessagePresenterImpl {
        return SibMessagePresenterImpl(this)
    }

    override fun getSibRootView(): View {
        binding = ActMessageBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initSibView() {
        setSibTitleBarVisibility(View.VISIBLE)
        setSibBackBtnRes(R.mipmap.pic_back_theme)
        setSibRightBtnRes(R.mipmap.pic_kefu_theme)
        setSibTextTitle(getString(R.string.message_title))

        initSibRefreshLayout()
        initSibRecyclerView()
    }

    override fun initSibListener() {
        //联系客服
        rootViewBinding.ivFun.setOnClickListener {
            showSibKefuDialog("Message Page")
            HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_CUSTOMER,"Message Page")
        }
    }

    override fun initSibData() {
        showSibLoading()
        mvpPresenter.loginSibMessageListAction()
    }

    /* 初始化刷新 */
    private fun initSibRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(false)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
             mvpPresenter.loginSibMessageListAction()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            binding.refreshLayout.finishLoadMore()
        }
    }

    /**
     * 初始化消息列表
     */
    private fun initSibRecyclerView() {
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = false
        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        messageAdapter = SibMessageAdapter(binding.recyclerView)
        binding.recyclerView.adapter = messageAdapter!!
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        messageAdapter!!.setOnItemChildClickListener { _, childView, position ->
            val item = messageAdapter!!.getItem(position)
            when(item.optString("type")){
                "REPAY_REMIDER",//还款提醒
                "APPLY_STATUS_UPDATE" -> {//申请状态更新
                    //进入还款页
                    EventBus.getDefault().post(SibEventData(SibEventData.TYPE.SIB_SHOW_REPAY))
                    ActivityStackManager.finishAllActivity(SibMainAct::class.java)
                }
                else -> {//AD_MESSAGE  广告推广
                    val stepData = JSONObject(SharedHelper.getSibSPString(SharedHelper.SIB_SPKEY.SIBLINGS_PROCESS))
                    if (stepData.optJSONArray("steps")!!.length() == 0) {//流程已完成 - 进入首页
                        //进入还款页
                        EventBus.getDefault().post(SibEventData(SibEventData.TYPE.SIB_SHOW_HOME))
                        ActivityStackManager.finishAllActivity(SibMainAct::class.java)
                    } else {
                        //流程未完成 - 进入流程页面
                        //上报
                        TestDataLibInvoke.sycData()
                        onIntent(SibStepsAct::class.java)
                        finish()
                    }
                }
            }
        }
    }

    /**
     * 显示客服dialog
     */
    private fun showSibKefuDialog(eventRemark: String){
        val kefuDialog = KefuDialog(this,eventRemark,object : KefuDialog.IDialogCallback{
            override fun onClick(view: View?) {
            }
        })
        kefuDialog.setCancelable(true)
        kefuDialog.setCanceledOnTouchOutside(true)
        kefuDialog.show()
    }

    /**
     * 设置消息列表
     */
    override fun showSibMessageList(msgList: ArrayList<JSONObject>) {
        messageAdapter!!.data = msgList
    }

    override fun hideSibLoading() {
        super.hideSibLoading()
        binding.refreshLayout.finishRefresh()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_MESSAGE_START,"")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_MESSAGE_END,"")
    }
}